package com.fellansoft.moviescollection.gui;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import com.fellansoft.moviescollection.R;
import com.fellansoft.moviescollection.gui.GuiUtilities;
import com.fellansoft.moviescollection.utils.ApplicationVariables;

import java.io.IOException;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Naty    ->Naty
 * Date: 5/12/12
 * Time: 9:05 PM
 * To change this template use File | Settings | File Templates.
 */
public class ImageAdapterControlGrid extends BaseAdapter {
    /**
     * Inflater for the gird element
     */
    private LayoutInflater mInflater;
    /**
     * List of URIs for the grid view
     */
    private List<Uri> uriList;
    /**
     * Current context
     */
    Context context;

    public ImageAdapterControlGrid(Context context, List<Uri> uriL) {
        //create the inflater just once
        uriList = uriL;
        this.context = context;
        mInflater = LayoutInflater.from(context);

    }

    public int getCount() {
        return this.uriList.size();
    }

    public Object getItem(int position) {
        return uriList.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    //making a view for each row
    public View getView(int position, View convertView, ViewGroup parent) {
        // A ViewHolder keeps references to children views to avoid unneccessary calls
        // to findViewById() on each row.

        ImageView holder;
        // When convertView is not null, we can reuse it directly, there is no need
        // to reinflate it. We only inflate a new View when the convertView supplied
        // by Grid is null.
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.imageview, null);
            // Creates a ViewHolder and store references to the two children views
            // we want to bind data to.
            holder = new ImageView(context);
            holder = (ImageView) convertView.findViewById(R.id.grid_item_image);
            convertView.setTag(holder);
        } else {
            // Get the ViewHolder back to get fast access to the TextView
            // and the ImageView.
            holder = (ImageView) convertView.getTag();
        }

        // Bind the data efficiently with the holder.

        try {
            holder.setImageBitmap(GuiUtilities.getThumbnailFromUri(uriList.get(position), ApplicationVariables.getDisplayWidth()/4));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return convertView;
    }

}